/**
 * PHOSIDE: PHosphorylation Site IDentification Engine.
 * Copyright 2009 Digital Biology Lab, University of Missouri.
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version. <p/> This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License for more
 * details.
 */

package phoside.ui;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import java.io.File;
import java.io.IOException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import phoside.PhosideInit;
import phoside.PhosphoProteins;
import phoside.PhosphoProteinsImpl;

import phoside.classify.result.PhosidePredictionResult;

import phoside.model.PhosideModel;

import phoside.io.ProteinsReader;
import phoside.io.fasta.PhosphoReaderFromFastaWithSitesInSequence;
import phoside.io.xml.PhosideXmlProteinsReader;
import phoside.io.xml.PhosphoProteinFieldValueFormatter;

import phoside.ui.task.DeserializeTask;
import phoside.ui.task.ModelLoadingTask;
import phoside.ui.task.ProteinsReadTask;
import phoside.ui.task.TaskUtil;
import phoside.ui.util.JTabbedPaneWithCloseIcons;

import phoside.util.FileExtensionsFilter;
import phoside.util.FilePathParser;
import phoside.util.StaticFinalValues;

/**
 *
 * @author gjj
 */
public class PhosideDesktop extends javax.swing.JFrame {

    /** Creates new form PhosideMainUI */
    public PhosideDesktop() {
        predictionPanel = new PhosidePredictionPanel();
        resultPanels = new ArrayList();
        initComponents();
        setupMainTabPane();
        installCloseHandler();
    }

    private void installCloseHandler() {
        mainTabbedPane.addTabActionListener(JTabbedPaneWithCloseIcons.DELETE_ACTION,
                new JTabbedPaneWithCloseIcons.TabActionListener() {
            public void actionOnTab(int tabNumber) {
                PhosideResultPanel panel = resultPanels.get(tabNumber-1); // the first one is the prediction panel
                if (!panel.isSaved()) {
                    int ret = JOptionPane.showConfirmDialog(PhosideDesktop.this, "Would you like to save the prediction result before closing?", "Save?", JOptionPane.YES_NO_CANCEL_OPTION);
                    if (ret == JOptionPane.CANCEL_OPTION) {
                        return;
                    } else if (ret == JOptionPane.YES_OPTION) {
                        if (!panel.saveResult(false)) {
                            return;
                        }
                    }
                }
                
                panel.dispose();
                removeResultPanel(panel);
            }            
        });

        mainTabbedPane.addTabActionListener(JTabbedPaneWithCloseIcons.RENAME_ACTION,
                new JTabbedPaneWithCloseIcons.TabActionListener() {
            public void actionOnTab(int tabNumber) {
                PhosideResultPanel panel = resultPanels.get(tabNumber-1); // the first one is the prediction panel
                JTextField textField = textField = new JTextField(10);
                textField.setText(panel.getPanelName());
                int ret = JOptionPane.showOptionDialog(PhosideDesktop.this, textField,
                        "Rename to", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
                        null, null, null);
                if(ret == JOptionPane.OK_OPTION) {
                    String name = textField.getText();
                    if (name==null||name.length()==0) {
                        JOptionPane.showMessageDialog(PhosideDesktop.this, "Name can not be empty.");
                        return;
                    }
                    mainTabbedPane.setTitleAt(tabNumber, name);
                    panel.setPanelName(name);
               }
            }
        });

        // deactivate default handler
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

        // now install
        addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            boolean saved = true;
            for (PhosideResultPanel panel : resultPanels) {
                if (!panel.isSaved()) {
                    saved = false;
                    break;
                }
            }

            if (saved)
                System.exit(0);

            int result = JOptionPane.showConfirmDialog(PhosideDesktop.this,
                    "Would you like to save the predicted result(s) before exiting PHOSIDE?",
                    "Save before exit?", JOptionPane.YES_NO_CANCEL_OPTION);
            if (result == JOptionPane.NO_OPTION) {
                System.exit(0);
            } else if (result == JOptionPane.YES_OPTION) {
                for (PhosideResultPanel panel : resultPanels) {
                    if (!panel.isSaved()) {
                        if (!panel.saveResult(false)) {
//                                int ret = JOptionPane.showConfirmDialog(PhosideDesktop.this,
//                                        "Would you still like to exit PHOSIDE?",
//                                        "Exit?", JOptionPane.YES_NO_OPTION);
//                                if (ret == JOptionPane.YES_OPTION) {
//                                    System.exit(0);
//                                } else
                            return;
                        }
                    }
                }

                System.exit(0);
            } 
        }
    });
}

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.JMenuBar PhosideMenuBar = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem manageModelMenuItem = new javax.swing.JMenuItem();
        jSeparator5 = new javax.swing.JSeparator();
        javax.swing.JMenuItem loadResultMenuItem = new javax.swing.JMenuItem();
        saveResultMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu toolsMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem trainMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu featureExtractionMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem disorderMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu fileProcessingMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem fasta2xmlMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenuItem xml2fastaMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenuItem uniprot2phosideMenuItem = new javax.swing.JMenuItem();
        javax.swing.JSeparator jSeparator1 = new javax.swing.JSeparator();
        javax.swing.JMenuItem accessionExportMenuItem = new javax.swing.JMenuItem();
        jSeparator4 = new javax.swing.JSeparator();
        javax.swing.JMenuItem extractSurrMenuItem = new javax.swing.JMenuItem();
        jSeparator6 = new javax.swing.JPopupMenu.Separator();
        javax.swing.JMenuItem filterFileMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenuItem nrBuildMenuItem = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JSeparator();
        javax.swing.JMenuItem mergeMenuItem = new javax.swing.JMenuItem();
        jSeparator3 = new javax.swing.JSeparator();
        javax.swing.JMenuItem fileStatisticsMenuItem = new javax.swing.JMenuItem();
        testToolsMenu = new javax.swing.JMenu();
        recalSpecMenuItem = new javax.swing.JMenuItem();
        modelRocMenuItem = new javax.swing.JMenuItem();
        resRocMenuItem = new javax.swing.JMenuItem();
        helpMenu = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("PHOSIDE");
        getContentPane().setLayout(new java.awt.GridBagLayout());

        fileMenu.setText("File");
        fileMenu.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                fileMenuMouseEntered(evt);
            }
        });

        manageModelMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_M, java.awt.event.InputEvent.CTRL_MASK));
        manageModelMenuItem.setText("Manage Trained Models");
        manageModelMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                manageModelMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(manageModelMenuItem);
        fileMenu.add(jSeparator5);

        loadResultMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        loadResultMenuItem.setText("Open Predicted Result");
        loadResultMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loadResultMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(loadResultMenuItem);

        saveResultMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        saveResultMenuItem.setText("Save Predicted Result");
        saveResultMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveResultMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(saveResultMenuItem);

        PhosideMenuBar.add(fileMenu);

        toolsMenu.setText("Tools");

        trainMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_T, java.awt.event.InputEvent.CTRL_MASK));
        trainMenuItem.setText("Model Training");
        trainMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                trainMenuItemActionPerformed(evt);
            }
        });
        toolsMenu.add(trainMenuItem);

        featureExtractionMenu.setText("Feature Extraction");

        disorderMenuItem.setText("Disorder Prediction");
        disorderMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                disorderMenuItemActionPerformed(evt);
            }
        });
        featureExtractionMenu.add(disorderMenuItem);

        toolsMenu.add(featureExtractionMenu);

        fileProcessingMenu.setText("File Processing");

        fasta2xmlMenuItem.setText("Convert FASTA to PHOSIDE XML");
        fasta2xmlMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fasta2xmlMenuItemActionPerformed(evt);
            }
        });
        fileProcessingMenu.add(fasta2xmlMenuItem);

        xml2fastaMenuItem.setText("Convert PHOSIDE XML to FASTA");
        xml2fastaMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                xml2fastaMenuItemActionPerformed(evt);
            }
        });
        fileProcessingMenu.add(xml2fastaMenuItem);

        uniprot2phosideMenuItem.setText("Convert UniProt XML to PHOSIDE XML");
        uniprot2phosideMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                uniprot2phosideMenuItemActionPerformed(evt);
            }
        });
        fileProcessingMenu.add(uniprot2phosideMenuItem);
        fileProcessingMenu.add(jSeparator1);

        accessionExportMenuItem.setText("Export Accessions from PHOSIDE XML");
        accessionExportMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                accessionExportMenuItemActionPerformed(evt);
            }
        });
        fileProcessingMenu.add(accessionExportMenuItem);
        fileProcessingMenu.add(jSeparator4);

        extractSurrMenuItem.setText("Extract surrounding sequences of sites");
        extractSurrMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                extractSurrMenuItemActionPerformed(evt);
            }
        });
        fileProcessingMenu.add(extractSurrMenuItem);
        fileProcessingMenu.add(jSeparator6);

        filterFileMenuItem.setText("Filter PHOSIDE XML File");
        filterFileMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                filterFileMenuItemActionPerformed(evt);
            }
        });
        fileProcessingMenu.add(filterFileMenuItem);

        nrBuildMenuItem.setText("Build Non-Redundant Dataset");
        nrBuildMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nrBuildMenuItemActionPerformed(evt);
            }
        });
        fileProcessingMenu.add(nrBuildMenuItem);
        fileProcessingMenu.add(jSeparator2);

        mergeMenuItem.setText("Merge PHOSIDE XML Files");
        mergeMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mergeMenuItemActionPerformed(evt);
            }
        });
        fileProcessingMenu.add(mergeMenuItem);
        fileProcessingMenu.add(jSeparator3);

        fileStatisticsMenuItem.setText("File Statistics");
        fileStatisticsMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fileStatisticsMenuItemActionPerformed(evt);
            }
        });
        fileProcessingMenu.add(fileStatisticsMenuItem);

        toolsMenu.add(fileProcessingMenu);

        testToolsMenu.setVisible(false);
        testToolsMenu.setText("Result Interpretation");
        testToolsMenu.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                testToolsMenuMouseEntered(evt);
            }
        });

        recalSpecMenuItem.setText("Recaculate specificities for current result by submitting ground truth");
        recalSpecMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                recalSpecMenuItemActionPerformed(evt);
            }
        });
        testToolsMenu.add(recalSpecMenuItem);

        modelRocMenuItem.setText("Get ROC information of the current selected prediction model");
        modelRocMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                modelRocMenuItemActionPerformed(evt);
            }
        });
        testToolsMenu.add(modelRocMenuItem);

        resRocMenuItem.setText("Get ROC information of the current prediction result");
        resRocMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resRocMenuItemActionPerformed(evt);
            }
        });
        testToolsMenu.add(resRocMenuItem);

        toolsMenu.add(testToolsMenu);

        PhosideMenuBar.add(toolsMenu);

        helpMenu.setText("Help");
        PhosideMenuBar.add(helpMenu);

        setJMenuBar(PhosideMenuBar);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void trainMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_trainMenuItemActionPerformed
        PhosideTrainDialog dialog = new PhosideTrainDialog(this, true);
        dialog.setLocationRelativeTo(this);
        dialog.setVisible(true);
    }//GEN-LAST:event_trainMenuItemActionPerformed

    private void manageModelMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_manageModelMenuItemActionPerformed
        ModelManageFrame frame = new ModelManageFrame();
        frame.setLocationRelativeTo(this);
        frame.setVisible(true);
    }//GEN-LAST:event_manageModelMenuItemActionPerformed

    private void disorderMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_disorderMenuItemActionPerformed
        DisorderPredictionDialog dialog = new DisorderPredictionDialog(this, true);
        dialog.setLocationRelativeTo(this);
        dialog.setVisible(true);
    }//GEN-LAST:event_disorderMenuItemActionPerformed

    private void fasta2xmlMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fasta2xmlMenuItemActionPerformed
        Fasta2XmlDialog dialog = new Fasta2XmlDialog(this, true);
        dialog.setLocationRelativeTo(this);
        dialog.setVisible(true);
    }//GEN-LAST:event_fasta2xmlMenuItemActionPerformed

    private void uniprot2phosideMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_uniprot2phosideMenuItemActionPerformed
        Uniprot2PhosideDialog dialog = new Uniprot2PhosideDialog(this, true);
        dialog.setLocationRelativeTo(this);
        dialog.setVisible(true);
    }//GEN-LAST:event_uniprot2phosideMenuItemActionPerformed

    private void filterFileMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_filterFileMenuItemActionPerformed
        ProteinsFilterDialog dialog = new ProteinsFilterDialog(this, true);
        dialog.setLocationRelativeTo(this);
        dialog.setVisible(true);
    }//GEN-LAST:event_filterFileMenuItemActionPerformed

    private void xml2fastaMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_xml2fastaMenuItemActionPerformed
        Xml2FastaDialog dialog = new Xml2FastaDialog(this, true);
        dialog.setLocationRelativeTo(this);
        dialog.setVisible(true);
    }//GEN-LAST:event_xml2fastaMenuItemActionPerformed

    private void fileStatisticsMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fileStatisticsMenuItemActionPerformed
        FileStatisticsDialog dialog = new FileStatisticsDialog(this, true);
        dialog.setLocationRelativeTo(this);
        dialog.setVisible(true);
    }//GEN-LAST:event_fileStatisticsMenuItemActionPerformed

    private void mergeMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mergeMenuItemActionPerformed
        MergeFileDialog dialog = new MergeFileDialog(this, true);
        dialog.setLocationRelativeTo(this);
        dialog.setVisible(true);
    }//GEN-LAST:event_mergeMenuItemActionPerformed

    private void nrBuildMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nrBuildMenuItemActionPerformed
        ProteinsNRBuildDialog dialog = new ProteinsNRBuildDialog(this, true);
        dialog.setLocationRelativeTo(this);
        dialog.setVisible(true);
    }//GEN-LAST:event_nrBuildMenuItemActionPerformed

    private void loadResultMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadResultMenuItemActionPerformed
        JFileChooser fc = new JFileChooser(PhosideInit.defaultPath);

        ArrayList<String> exts = new ArrayList<String>(1);
//        String xml = "xml";
//        exts.add(xml);
//        fc.addChoosableFileFilter(new FileExtensionsFilter(exts,"PHOSIDE XML file (.xml)"));
//
//        exts = new ArrayList<String>(1);
        String pred = "pred";
        exts.add(pred);
        fc.addChoosableFileFilter(new FileExtensionsFilter(exts,"PHOSIDE PREDICTION file (.pred)"));

        fc.setDialogTitle("Select a file...");
        int returnVal = fc.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            PhosideInit.defaultPath = file.getParent();

            String filePath = PhosideInit.defaultPath + File.separator + file.getName();

            Map<String,PhosidePredictionResult> result;
            if (filePath.toLowerCase().endsWith(pred)) {
                DeserializeTask task = new DeserializeTask(filePath,"gz");
                TaskUtil.execute(task);
                if (!task.success()) {
                    JOptionPane.showMessageDialog(this, "Failed to load the result.");
                    return;
                }

                Object obj = task.getObject();
                if (obj instanceof Map) {
                    result = (Map<String,PhosidePredictionResult>)obj;
                } else if (obj instanceof PhosidePredictionResult) {
                    result = new java.util.LinkedHashMap(1);
                    result.put("", (PhosidePredictionResult)obj);
                } else {
                    JOptionPane.showMessageDialog(this, "Failed to load the result.");
                    return;
                }

                
//            } else if (filePath.toLowerCase().endsWith(xml)) {
//                result = new PhosidePredictionResultImpl();
//                PhosideXmlProteinsReader readerPredicted = new PhosideXmlProteinsReader(filePath, result,
//                        new PhosideResultFieldValueFormatter());
//
//                try {
//                    readerPredicted.read();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                    JOptionPane.showMessageDialog(this, "Failed to read the file.");
//                    return;
//                }

            } else {
                JOptionPane.showMessageDialog(this, "Only .pred is supported.");
                return;
            }            

            String name = FilePathParser.getName(filePath);
            try {
                PhosideResultPanel resultPanel
                        = new PhosideResultPanel(name, result, true);
                addResultPanel(resultPanel);
            } catch (Exception e) {
                e.printStackTrace();
                JOptionPane.showMessageDialog(this, "Failed to load!");
                return;
            }
            JOptionPane.showMessageDialog(this, "Loaded!");
        }
    }//GEN-LAST:event_loadResultMenuItemActionPerformed

    private void testToolsMenuMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_testToolsMenuMouseEntered
        boolean resultTabSelected = mainTabbedPane.getSelectedIndex()>0;
        recalSpecMenuItem.setEnabled(resultTabSelected);
        resRocMenuItem.setEnabled(resultTabSelected);
        modelRocMenuItem.setEnabled(predictionPanel.getSelectedModel()!=null);
    }//GEN-LAST:event_testToolsMenuMouseEntered

    private void recalSpecMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_recalSpecMenuItemActionPerformed
//        JFileChooser fc = new JFileChooser(PhosideInit.defaultPath);
//
//        ArrayList<String> exts = new ArrayList<String>(1);
//        String fasta = "fasta";
//        exts.add(fasta);
//        fc.addChoosableFileFilter(new FileExtensionsFilter(exts,"Fasta file (.fasta)"));
//
//        exts = new ArrayList<String>(1);
//        String xml = "xml";
//        exts.add(xml);
//        fc.addChoosableFileFilter(new FileExtensionsFilter(exts,"Xml file (.xml)"));
//
//        fc.setDialogTitle("Select a file...");
//        int returnVal = fc.showOpenDialog(this);
//        if (returnVal == JFileChooser.APPROVE_OPTION) {
//            File file = fc.getSelectedFile();
//            PhosideInit.defaultPath = file.getParent();
//
//            String filePath = PhosideInit.defaultPath + File.separator + file.getName();
//            String ext = FilePathParser.getExt(filePath);
//
//            PhosphoProteins groundTruth = new PhosphoProteinsImpl();
//            ProteinsReader phosphoReader;
//
//            if (ext.equalsIgnoreCase("xml")) {
//                phosphoReader = new PhosideXmlProteinsReader(filePath,
//                        groundTruth, new PhosphoProteinFieldValueFormatter());
//            } else if (ext.equalsIgnoreCase("fasta")) {
//                try {
//                   phosphoReader = new PhosphoReaderFromFastaWithSitesInSequence(filePath,
//                                groundTruth,StaticFinalValues.DEFAULTHEADERRULE);
//                } catch (IOException e) {
//                    e.printStackTrace();
//                    JOptionPane.showMessageDialog(this, "Failed to read the fasta file");
//                    return;
//                }
//            } else {
//                JOptionPane.showMessageDialog(this, "Only xml or fasta files are allowed.");
//                return;
//            }
//
//            ProteinsReadTask phosphoReadTask = new ProteinsReadTask(phosphoReader);
//            TaskUtil.execute(phosphoReadTask);
//            if (!phosphoReadTask.success()) {
//                JOptionPane.showMessageDialog(this, "Failed to read the file");
//                return;
//            }
//
//            int iTab = mainTabbedPane.getSelectedIndex();
//            PhosideResultPanel panel = getResultPanels().get(iTab-1);
//            panel.setGroundTruth(groundTruth);
//        }
    }//GEN-LAST:event_recalSpecMenuItemActionPerformed

    private void modelRocMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modelRocMenuItemActionPerformed
//        ModelLoadingTask modelLoadingTask = new ModelLoadingTask(predictionPanel.getSelectedModel());
//        TaskUtil.execute(modelLoadingTask);
//        if (!modelLoadingTask.success()) {
//            JOptionPane.showMessageDialog(this, "Error: failed to load model.");
//            return;
//        }
//
//        List<PhosideModel> model = modelLoadingTask.getModel();
//
//        if (model==null || model.size()==0) {
//            JOptionPane.showMessageDialog(this, "Error: failed to load model.");
//            return;
//        }
//
//        // TODO: display all
//        phoside.classify.result.ROC roc = model.get(0).getROC();
//        if (roc==null) {
//            JOptionPane.showMessageDialog(this, "There is no ROC information in the selected model.");
//            return;
//        }
//
//        ViewROCDialog dialog = new ViewROCDialog(this, true,
//                new phoside.classify.result.ResultInterpreter(roc));
//        dialog.setLocationRelativeTo(this);
//        dialog.setVisible(true);
    }//GEN-LAST:event_modelRocMenuItemActionPerformed

    private void resRocMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resRocMenuItemActionPerformed
//        int iTab = mainTabbedPane.getSelectedIndex();
//        PhosideResultPanel panel = getResultPanels().get(iTab-1);
//        phoside.classify.result.ResultInterpreter ri = panel.getResult().getResultInterpreter();
//        if (ri==null) {
//            JOptionPane.showMessageDialog(this, "There is no ROC information for the current result.");
//            return;
//        }
//
//        ViewROCDialog dialog = new ViewROCDialog(this, true, ri);
//        dialog.setLocationRelativeTo(this);
//        dialog.setVisible(true);
    }//GEN-LAST:event_resRocMenuItemActionPerformed

    private void accessionExportMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_accessionExportMenuItemActionPerformed
        AccessionExportDialog dialog = new AccessionExportDialog(this,true);
        dialog.setLocationRelativeTo(this);
        dialog.setVisible(true);
    }//GEN-LAST:event_accessionExportMenuItemActionPerformed

    private void fileMenuMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_fileMenuMouseEntered
        boolean resultTabSelected = mainTabbedPane.getSelectedIndex()>0;
        saveResultMenuItem.setEnabled(resultTabSelected);
    }//GEN-LAST:event_fileMenuMouseEntered

    private void saveResultMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveResultMenuItemActionPerformed
        int iTab = mainTabbedPane.getSelectedIndex();
        PhosideResultPanel panel = getResultPanels().get(iTab-1);
        panel.saveResult(true);
    }//GEN-LAST:event_saveResultMenuItemActionPerformed

    private void extractSurrMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_extractSurrMenuItemActionPerformed
        SurroundSequenceExtractDialog dialog = new SurroundSequenceExtractDialog(this,true);
        dialog.setLocationRelativeTo(this);
        dialog.setVisible(true);
    }//GEN-LAST:event_extractSurrMenuItemActionPerformed

    private void setupMainTabPane() {
        mainTabbedPane = new phoside.ui.util.JTabbedPaneWithCloseIcons();
        
        mainTabbedPane.setMinimumSize(new java.awt.Dimension(400, 300));
        mainTabbedPane.setPreferredSize(new java.awt.Dimension(800, 600));

        mainTabbedPane.addTab("Prediction Panel", predictionPanel);

        java.awt.GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        getContentPane().add(mainTabbedPane, gridBagConstraints);
        pack();
    }

    public PhosidePredictionPanel getPredictionPanel() {
        return predictionPanel;
    }

    public List<PhosideResultPanel> getResultPanels() {
        return resultPanels;
    }

    void addResultPanel(PhosideResultPanel panel) {
        resultPanels.add(panel);
        mainTabbedPane.addClosableTab(panel.getPanelName(), panel);
        mainTabbedPane.setSelectedComponent(panel);
    }

    void removeResultPanel(PhosideResultPanel panel) {
        int idx = resultPanels.indexOf(panel);
        if (idx!=-1) {
            mainTabbedPane.removeTabAt(idx+1);
            resultPanels.remove(panel);
        }

        //mainTabbedPane.setSelectedIndex(0);
    }

    private PhosidePredictionPanel predictionPanel;
    private List<PhosideResultPanel> resultPanels;
    private phoside.ui.util.JTabbedPaneWithCloseIcons mainTabbedPane;
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu fileMenu;
    private javax.swing.JMenu helpMenu;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JSeparator jSeparator5;
    private javax.swing.JPopupMenu.Separator jSeparator6;
    private javax.swing.JMenuItem modelRocMenuItem;
    private javax.swing.JMenuItem recalSpecMenuItem;
    private javax.swing.JMenuItem resRocMenuItem;
    private javax.swing.JMenuItem saveResultMenuItem;
    private javax.swing.JMenu testToolsMenu;
    // End of variables declaration//GEN-END:variables

}
